const express = require('express');
const router = express.Router();
const UserService = require('./user.service');
const { validateUser } = require('./user.validation');

/**
 * User Controller V1
 * Legacy user management endpoints
 * @deprecated Use V2 instead
 */

// GET /api/v1/users - Get all users (legacy format)
router.get('/', async (req, res, next) => {
  try {
    const users = await UserService.getAllUsers();
    
    // V1 legacy response format
    res.json({
      success: true,
      data: users,
      count: users.length
    });
  } catch (error) {
    next(error);
  }
});

// GET /api/v1/users/:id - Get user by ID (legacy format)
router.get('/:id', async (req, res, next) => {
  try {
    const user = await UserService.getUserById(req.params.id);
    
    if (!user) {
      return res.status(404).json({
        success: false,
        message: 'User not found'
      });
    }

    // V1 legacy response format
    res.json({
      success: true,
      data: user
    });
  } catch (error) {
    next(error);
  }
});

// POST /api/v1/users - Create user (legacy format)
router.post('/', validateUser, async (req, res, next) => {
  try {
    const newUser = await UserService.createUser(req.body);
    
    // V1 legacy response format
    res.status(201).json({
      success: true,
      message: 'User created successfully',
      data: newUser
    });
  } catch (error) {
    next(error);
  }
});

module.exports = router;